import sortedcontainers
class Solution:
    def numberOfPairs(self, points) -> int:
        n = len(points)
        new_points = [[y, x] for x, y in points]
        st = sortedcontainers.SortedList(new_points)
        points.sort()
        index = 1
        last_value = points[0][0]
        now_list = [points[0]]
        ans = 0
        while index < n:
            if points[index][0] == last_value:
                now_list.append(points[index])
            else:
                now_list = now_list[::-1]
                y_up = st.bisect_right([y, float('inf')])
                current_y = []
                for j in range(y_up - 1, -1, -1):
                    if current_y:
                        if st[j][1] < current_y[-1][1]:
                            if st[j][0] == current_y[-1][0]:
                                current_y[-1][1] = st[j][1]
                            else:
                                current_y.append(st[j])
                index0 = 0
                index1 = 0
                while index0 < len(now_list) and index1 < len(current_y):
                               
                last_value = points[index][0]
                now_list = [points[index]]
            index += 1
        now_list = now_list[::-1]
        for x, y in now_list:
            y_up = st.bisect_right([y, float('inf')])
            last_x_max = float('inf')
            for j in range(y_up - 1, -1, -1):
                if [st[j][1], st[j][0]] == [x, y]:
                    continue            
                for j in range(y_up - 1, -1, -1):
                    if st[j][1] < last_x_max:
                        ans += 1
                        last_x_max = st[j][1]         
            st.remove([y, x])          
        return ans

data = Solution()
# points = [[1,1],[2,2],[3,3]]
# print(data.numberOfPairs(points))      
# points = [[6,2],[4,4],[2,6]]
# print(data.numberOfPairs(points))    
points = [[3,1],[1,3],[1,1]] 
print(data.numberOfPairs(points))   
points = [[2,6],[2,4]]
print(data.numberOfPairs(points))